package org.ohos.rajawali3d.examples.examples.interactive;

import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import opengl.platform.ohos.Log;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.lights.DirectionalLight;
import org.rajawali3d.loader.LoaderAWD;
import org.rajawali3d.materials.Material;
import org.rajawali3d.materials.methods.DiffuseMethod;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.util.ObjectColorPicker;

public class ObjectRotateFragment extends AExampleFragment {

    private Text textMixed;
    private Text textY;
    private Text textHorizontal;
    private Slider seekBarMixed;
    private Slider seekBarY;
    private Slider seekBarHoizontal;

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        Component components = inflater.parse(ResourceTable.Layout_object_roate_overlay, mLayout, true);
        textMixed = (Text) container.findComponentById(ResourceTable.Id_textMixed);
        textY = (Text) container.findComponentById(ResourceTable.Id_textY);
        textHorizontal = (Text) container.findComponentById(ResourceTable.Id_textHorizontal);
        seekBarMixed = (Slider) container.findComponentById(ResourceTable.Id_seekBarMixed);
        seekBarY = (Slider) container.findComponentById(ResourceTable.Id_seekBarY);
        seekBarHoizontal = (Slider) container.findComponentById(ResourceTable.Id_seekBarHoizontal);
        textMixed.setClickedListener(component -> seekBarMixed.setProgressValue(0));
        textY.setClickedListener(component -> seekBarY.setProgressValue(0));
        textHorizontal.setClickedListener(component -> seekBarHoizontal.setProgressValue(0));

        seekBarMixed.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                ((ObjectRotateRenderer) mRenderer).rotateDataX(Double.valueOf(i));
                textMixed.setText("Z=" + i);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });

        seekBarY.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                ((ObjectRotateRenderer) mRenderer).rotateDataY(Double.valueOf(i));
                textY.setText("Y=" + i);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });

        seekBarHoizontal.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {
                ((ObjectRotateRenderer) mRenderer).rotateDataZ(Double.valueOf(i));
                textHorizontal.setText("x=" + i);
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
            }
        });
        mLayout.moveChildToFront(components);
        mRenderSurface.setTransparent(false);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        return mLayout;
    }

    @Override
    public AExampleRenderer createRenderer() {
        return new ObjectRotateRenderer(getContext(), this);
    }

    private final class ObjectRotateRenderer extends AExampleRenderer {
        private DirectionalLight light;
        private Object3D monkey;
        private ObjectColorPicker picker;

        public ObjectRotateRenderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        @Override
        protected void initScene() {
            try {
                picker = new ObjectColorPicker(this);
                light = new DirectionalLight(-1.0, 0.0, 1.0);
                light.setPosition(0.0, 0.0, -4.0);
                light.setPower(1.5f);
                getCurrentScene().addLight(light);
                getCurrentCamera().setPosition(0.0, 0.0, 7.0);
                Material material = new Material();
                material.enableLighting(true);
                material.setDiffuseMethod(new DiffuseMethod.Lambert());
                ResourceManager resManager = mContext.getResourceManager();
                RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/awd_suzanne.awd");

                final LoaderAWD parser = new LoaderAWD(mContext.getResourceManager(), mTextureManager, rawFileEntry);
                parser.parse(mContext);

                // Inserting a couple nested containers to test child picking;
                // should appear/behave the same
                Object3D container = new Object3D();
                getCurrentScene().addChild(container);
                Object3D container1 = new Object3D();
                container1.setScale(.7);
                container1.setPosition(0.0, 0.0, 0.0);
                container.addChild(container1);
                monkey = parser.getParsedObject();
                monkey.setRotY(0.0);
                monkey.setMaterial(material);
                monkey.setColor(0x0000ff);
                container1.addChild(monkey);
                picker.registerObject(monkey);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onRender(long elapsedRealtime, double deltaTime) {
            super.onRender(elapsedRealtime, deltaTime);
            Log.d("rotation", "X=${monkey.rotX.round(1)} Y=${monkey.rotY.round(1)} Z=${monkey.rotZ.round(1)}");
        }

        public void rotateDataX(Double value) {
            Log.d("set rotation", "X=" + value);
            monkey.setRotation(new Vector3(value, monkey.getRotY(), monkey.getRotZ()));
        }

        public void rotateDataY(Double value) {
            Log.d("set rotation", "Y=" + value);
            monkey.setRotation(new Vector3(monkey.getRotX(), value, monkey.getRotZ()));
        }

        public void rotateDataZ(Double value) {
            Log.d("set rotation", "Z=" + value);
            monkey.setRotation(new Vector3(monkey.getRotX(), monkey.getRotY(), value));
        }

    }

}
